package com.wzl.base;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import sun.jvm.hotspot.opto.InlineTree;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author weiZhiLin
 * @version 1.0
 * @date 2021/5/25 21:56
 */
class MainTest {

    @org.junit.jupiter.api.Test
    // json 转换
    void format() {

        String formData = "{\"formId\":\"2c9991f6799c62210179a3ab7e2a0ab5\",\"mainTblName\":\"goa_sup_task\",\"mainTblData\":[{\"key\":\"secret_class\",\"value\":\"4\"},{\"key\":\"task_name\",\"value\":\"333\"},{\"key\":\"jbsx\",\"value\":\"领导批示\"},{\"key\":\"host_unit\",\"value\":\"[{\\\"scopeName\\\":\\\"默认单位\\\",\\\"scopeType\\\":\\\"extsupunit\\\",\\\"scopeValue\\\":\\\"ff80808177299c1c017738dbbaad028f\\\",\\\"title\\\":\\\"默认单位\\\"}]\"}],\"subTbl\":[],\"relatedTbl\":[],\"trans\":false,\"moduleCode\":\"\",\"paramJson\":\"\"}";
        Map<String, Object> parse = (Map<String, Object>) JSONObject.parse(formData);
//        ArrayList<Map<String, String>> mainTblData = (ArrayList<Map<String, String>>) parse.get("mainTblData");
        JSONArray mainTblData = (JSONArray) parse.get("mainTblData");
        String jbsxValue = "";
        for (int i = 0; i < mainTblData.size(); i++) {
            Map<String,String> map = (Map<String, String>)mainTblData.get(i);
            if ("jbsx".equals(map.get("key"))) {
                jbsxValue = map.get("value");
                break;
            }
        }
        for (int i = 0; i < mainTblData.size(); i++) {
            Map<String,String> mainTblDatum = (Map<String, String>)mainTblData.get(i);
            if ("task_name".equals(mainTblDatum.get("key"))) {
                mainTblDatum.put("value", jbsxValue);
                break;
            }
        }
        String s = JSONObject.toJSONString(parse);
        System.out.println(s);
        //        System.out.println(mainTblData);
       /* int i = formData.lastIndexOf("\"key\": \"jbsx\",");
        String substring = formData.substring(i);
        substring = substring.substring(substring.indexOf("value"), substring.indexOf("}"));
        System.out.println(substring);*/

    }
}